﻿#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>
#include <cmath>

std::string addStrings(std::string num1, std::string& num2) {
    std::reverse(num1.begin(), num1.end());
    std::reverse(num2.begin(), num2.end());

    int size = std::max(num1.size(), num2.size());
    int tempNum = 0;
    std::string result(size + 1, '0');

    for (int i = 0; i < size; i++) {
        tempNum = (i < num1.size() ? num1[i] - '0' : 0) + (i < num2.size() ? num2[i] - '0' : 0) + (result[i] - '0');
        if (tempNum < 10) {
            result[i] = tempNum + '0';
        }
        else {
            result[i] = tempNum % 10 + '0';
            result[i + 1] = (tempNum / 10) % 10 + '0';
        }
    }

    std::reverse(result.begin(), result.end());

    size_t startPos = result.find_first_not_of('0');
    if (startPos != std::string::npos) {
        result = result.substr(startPos);
    }
    else {
        result = "0";
    }

    return result;
}
void test() {
    std::cout << "test";
}

void test1(){
    std::cout << "牛魔酬宾" << std::endl;
}

std::string MUL(std::string num1, std::string num2) {
    std::reverse(num1.begin(), num1.end());
    std::reverse(num2.begin(), num2.end());

    int size = num1.size() + num2.size() + 2;
    int tempNum = 0;
    std::string result(size, '0');

    for (int i = 0; i < num1.size(); i++) {
        for (int j = 0; j < num2.size(); j++) {
            tempNum = (num1[i] - '0') * (num2[j] - '0');
            std::string tempNum_string(size, '0');
            tempNum_string[i + j] = tempNum % 10 + '0';
            tempNum_string[i + j + 1] = (tempNum / 10) % 10 + '0';

            std::reverse(tempNum_string.begin(), tempNum_string.end());

            result = addStrings(result, tempNum_string);
        }
    }


    size_t startPos = result.find_first_not_of('0');
    if (startPos != std::string::npos) {
        result = result.substr(startPos);
    }
    else {
        result = "0";
    }

    return result;
}

int main()
{
    std::cout << "输入两个整数\n";

    std::string num1;
    std::string num2;

    std::cin >> num1;
    std::cin >> num2;

    std::string result = MUL(num1, num2);

    std::cout << num1 << " ** " << num2 << " = " << result << "\n";

    test1();

    system("pause");
}


